Enviado por: Electros
Fecha: 2003-06-15 02:01:35
Actualizado: 2003-08-29 19:24:44
¿Usa base de datos?: 1
Visitas: 223
Votos: 0
Descargado: 428 veces
Valoración: 0.00
» Valora este script «
ZipDescargar

Ahora se podr�n enviar mensajes privados entre los usuarios de tu web. Primero debes crear la tabla en la base de datos, copia el siguiente c�digo y pegalo en el phpMyAdmin en SQL: [codigo]create table mensajes ( id smallint(5) unsigned not null auto_increment, nuevo tinyint(1) unsigned not null, fecha int(10) unsigned not null, remitente varchar(20) not null, destinatario varchar(20) not null, mensaje tinytext not null, primary key(id), index (destinatario) )[/codigo] Ahora debes crear el archivo mensajes.php con el siguiente c�digo: [codigo]<? include("ulogin.php") ; ?> <p><b>Mensajes</b> <? include("config.php") ; if($mensaje == "nuevo") { echo " <script> function revisar() { if(formulario.destinatario.value.length == 0) { alert('Debes escribir un destinatario') ; return false ; } if(formulario.mensaje.value.length == 0) { alert('Debes escribir un mensaje') ; return false ; } if(formulario.mensaje.value.length > 255) { alert('El mensaje supera los 255 caract�res') ; return false ; } } </script> <p><b>Nuevo</b> <p> <form name=formulario method=post action=mensajes.php onsubmit="return revisar()"> <b>Destinatario:</b><br> <input type=text name=destinatario maxlength=20><br> <b>Mensaje:</b><br> <textarea name=mensaje cols=30 rows=5></textarea><br><br> <input type=submit name=enviar value=Enviar> </form> " ; } else { echo "<p><a href=mensajes.php?mensaje=nuevo>Nuevo mensaje</a>" ; } if($responder) { echo " <script> function revisar() { if(formulario.destinatario.value.length == 0) { alert('Debes escribir un destinatario') ; return false ; } if(formulario.mensaje.value.length == 0) { alert('Debes escribir un mensaje') ; return false ; } if(formulario.mensaje.value.length > 255) { alert('El mensaje supera los 255 caract�res') ; return false ; } } </script> <p><b>Responder</b> <p> <form name=formulario method=post action=mensajes.php onsubmit="return revisar()"> <b>Destinatario:</b><br> <input type=text name=destinatario maxlength=20 value="$responder"><br> <b>Mensaje:</b><br> <textarea name=mensaje cols=30 rows=5></textarea><br><br> <input type=submit name=enviar value=Enviar> </form> "; } if($borrar) { $usuario = $_COOKIE["unick"] ; mysql_query("delete from mensajes where id='$borrar' and destinatario='$usuario'") ; echo "<p>El mensaje ha sido borrado con �xito. Haz click <a href=mensajes.php>aqu�</a> para regresar." ; } else { if($enviar) { function quitar($texto) { $texto = trim($texto) ; $texto = htmlspecialchars($texto) ; return $texto ; } $fecha = time() ; $destinatario = quitar($destinatario) ; $mensaje = quitar($mensaje) ; $resp = mysql_query("select id from usuarios where nick='$destinatario'") ; $datos = mysql_fetch_array($resp) ; if(mysql_num_rows($resp) == 0) { echo "<p>Este usuario no existe en la base de datos. Haz click <a href=javascript:history.back()>aqu�</a> para regresar."; } else { $remitente = $_COOKIE["unick"] ; mysql_query("insert into mensajes (fecha,destinatario,remitente,mensaje) values ('$fecha','$destinatario','$remitente','$mensaje')") ; echo "<p>El mensaje ha sido enviado con �xito. Haz click <a href=mensajes.php>aqu�</a> para regresar." ; } mysql_free_result($resp) ; } else { $usuario = $_COOKIE["unick"] ; $resp = mysql_query("select id from mensajes where destinatario='$usuario'") ; $mensajes = mysql_num_rows($resp) ; mysql_free_result($resp) ; $mostrar = 5 ; if(!$desde) { $desde = 0 ; } $resp = mysql_query("select * from mensajes where destinatario='$usuario' order by id desc limit $desde,$mostrar") ; $desde = $desde + $mostrar ; if(mysql_num_rows($resp) == 0) { echo "<p>No se encontraron mensajes." ; } else { echo " <p><b>Total de mensajes:</b> $mensajes <p> " ; while($datos = mysql_fetch_array($resp)) { $fecha = $datos[fecha] ; $mesesano = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto", "Septiembre","Octubre","Noviembre","Diciembre") ; $diames = date(j,$fecha) ; $mesano = date(n,$fecha) - 1 ; $ano = date(Y,$fecha) ; $hora = date("h:i A",$fecha) ; $fecha = "$diames $mesesano[$mesano] $ano $hora" ; echo " <table width=100% border=0 cellpadding=1 cellspacing=0> <tr> <td><b>$datos[remitente]</b></td> <td><div align=right><b>$fecha</b></div></td> </tr> <tr> <td colspan=2>$datos[mensaje]</td> </tr> <tr> <td colspan=2> <div align=right> <a href="mensajes.php?responder=$datos[remitente]">Responder</a> | <a href="mensajes.php?borrar=$datos[id]">Borrar</a> </div> </td> </tr> </table><br> " ; if($datos[nuevo] == 0) { mysql_query("update mensajes set nuevo='1' where id='$datos[id]'") ; } } echo " <p align=right><a href=mensajes.php?desde=$desde>Siguientes $mostrar mensajes</a> " ; } mysql_free_result($resp) ; } } mysql_close($conectar) ; ?>[/codigo] Para mostrar si hay mensajes nuevos y cu�ntos son, debes poner el siguiente c�digo por ejemplo en el men� para usuarios registrados: [codigo]<? include("config.php") ; $usuario = $HTTP_COOKIE_VARS[unick] ; $resp = mysql_query("select id from mensajes where nuevo='0' and destinatario='$usuario'") ; $mensajes = mysql_num_rows($resp) ; mysql_free_result($resp) ; mysql_close($conectar) ; if($mensajes == 0) { ?> <a href="mensajes.php">Mensajes</a><br><br> <? } else { ?> <script> function BlinkTxt() { texto = document.getElementsByTagName('blink'); for (i=0; i<texto.length; i++) if (texto[i].style.visibility=='hidden') { texto[i].style.visibility='visible'; } else { texto[i].style.visibility='hidden'; } setTimeout('BlinkTxt()',100); } onload=BlinkTxt; </script> <a href="mensajes.php"><blink>Mensajes (<? echo $mensajes ; ?>)</blink></a><br><br> <? } ?>[/codigo] Si no hay mensajes nuevos se muestra un enlace normal hacia el archivo mensajes.php, si llegar� a haber un mensaje o m�s, se muestra un enlace con el n�mero de mensajes recibidos y tambi�n se a�ade un efecto de brillo en javascript para que el usuario pueda ver con m�s facilidad cuando reciba mensajes nuevos. [b]Explicaci�n de su uso[/b] S�lo debes crear un enlace hacia el archivo mensajes.php para que desde ah� puedan se puedan enviar, responder y borrar mensajes. Listo el sistema de Registro de usuarios est� totalmente terminado, pronto habr� nuevos scripts como recuperaci�n de contrase�a y otros as�, as� que est�n pendientes con los nuevos scripts que vaya agregando. Para cualquier duda pueden entrar en el [url=http://electros.webcindario.com/ver.php?id=foro]foro[/url].